package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.PayLog;
import com.yunkeji.dao.mapper.PayLogMapper;
import com.yunkeji.dao.vo.PayLogVo;
import com.yunkeji.system.domain.bo.PayLogBo;
import com.yunkeji.system.service.IPayLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 支付日志Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class PayLogServiceImpl implements IPayLogService {

    private final PayLogMapper baseMapper;

    /** 查询支付日志 */
    @Override
    public PayLogVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询支付日志列表 */
    @Override
    public TableDataInfo<PayLogVo> queryPageList(PayLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayLog> lqw = buildQueryWrapper(bo);
//        Page<PayLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<PayLogVo> result = baseMapper.selectListPage(
            bo.getPlatformId(),
            bo.getUserId(),
            bo.getLogType(),
            bo.getSymbol(),
            bo.getBalanceType(),
            pageQuery.build()
        );
        return TableDataInfo.build(result);
    }

    /** 查询支付日志列表 */
    @Override
    public List<PayLogVo> queryList(PayLogBo bo) {
        LambdaQueryWrapper<PayLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayLog> buildQueryWrapper(PayLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlatformId() != null, PayLog::getPlatformId, bo.getPlatformId());
        lqw.eq(bo.getUserId() != null, PayLog::getUserId, bo.getUserId());
        lqw.in(bo.getLogType() != null, PayLog::getLogType, bo.getLogType());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbol()), PayLog::getSymbol, bo.getSymbol());
        lqw.eq(bo.getBalanceType() != null, PayLog::getBalanceType, bo.getBalanceType());
        lqw.eq(bo.getChangeNum() != null, PayLog::getChangeNum, bo.getChangeNum());
        lqw.eq(bo.getLogDate() != null, PayLog::getLogDate, bo.getLogDate());
        //只显示操作成功的支付记录
        lqw.eq(PayLog::getStatus, 1);
        lqw.orderByDesc(PayLog::getCreateTime);
        return lqw;
    }

    /** 新增支付日志 */
    @Override
    public Boolean insertByBo(PayLogBo bo) {
        PayLog add = BeanUtil.toBean(bo, PayLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改支付日志 */
    @Override
    public Boolean updateByBo(PayLogBo bo) {
        PayLog update = BeanUtil.toBean(bo, PayLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(PayLog entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除支付日志 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
